<?php

namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (isAdmin($request->user())) {
            $request->headers->add(['admin' => 1]);
        } else {
            $request->headers->add(['admin' => 0]);
        }
        $this->setMenus($request);
        $path = $request->path();
        if (! isAdmin($request->user()) && $request->method() == 'GET') {
            if (! in_array($path, ['admin/settings', 'admin/post/create', 'admin/posts'])) {
                return redirect('admin/settings');
            }
        }
        return $next($request);
    }

    /**
     * 根据权限设置菜单
     *
     * @param \Illuminate\Http\Request $request
     */
    protected function setMenus($request)
    {
        if (isAdmin($request->user())) {
            $menus = [
                [
                    'name' => 'Dashboard',
                    'icon' => 'tachometer',
                    'route' => 'admin.index'
                ],
                [
                    'name' => 'Writing',
                    'icon' => 'pencil',
                    'route' => 'post.create'
                ],
                [
                    'is_parent' => true,
                    'name' => 'Resources',
                    'icon' => 'list-alt',
                    'children' => [
                        [
                            'name' => 'Posts',
                            'icon' => 'book',
                            'route' => 'admin.posts'
                        ],
                        [
                            'name' => 'Pages',
                            'icon' => 'file-text',
                            'route' => 'admin.pages'
                        ],
                        [
                            'name' => 'Comments',
                            'icon' => 'comments',
                            'route' => 'admin.comments'
                        ],
                        [
                            'name' => 'Tags',
                            'icon' => 'tags',
                            'route' => 'admin.tags'
                        ],
                        [
                            'name' => 'Categories',
                            'icon' => 'folder',
                            'route' => 'admin.categories'
                        ],
                        [
                            'name' => 'Users',
                            'icon' => 'users',
                            'route' => 'admin.users'
                        ],
                        [
                            'name' => 'IP',
                            'icon' => 'internet-explorer',
                            'route' => 'admin.ips'
                        ],
                        [
                            'name' => 'Images',
                            'icon' => 'image',
                            'route' => 'admin.images'
                        ],
                        [
                            'name' => 'Files',
                            'icon' => 'file-archive-o',
                            'route' => 'admin.files'
                        ]
                    ]
                ],
                [
                    'name' => 'Settings',
                    'icon' => 'cog',
                    'route' => 'admin.settings'
                ],
                [
                    'name' => 'Application',
                    'icon' => 'cube',
                    'route' => 'admin.app'
                ],
            ];
        } else {
            $menus = [
                [
                    'name' => 'Settings',
                    'icon' => 'cog',
                    'route' => 'admin.settings'
                ],
                [
                    'name' => 'Writing',
                    'icon' => 'pencil',
                    'route' => 'post.create'
                ],
                [
                    'name' => 'Posts',
                    'icon' => 'book',
                    'route' => 'admin.posts'
                ],
            ];
        }
        $request->headers->add(['menus' => serialize($menus)]);
    }
}
